package se.slp.test1;


import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;


public class CreateAccount extends Activity {
	 /** Called when the activity is first created. */
	Intent launch_confirmdata;
	
	private EditText editText1; 
	private EditText editText2;
	private EditText editText3;
	private RadioButton male;
	private RadioButton female;
	
	private String strInput1;
	private String strInput2;
	private String strInput3;
	private String strInput4;
	private String strInput5;
	private String strInput6;
	private String strInput7;
	
	private Button btnSave;
	private DataHelper dh;
	private List<String> allData;
	private AlertDialog.Builder alert,alert2;
	
    double bmi = 1.0;
    double w = 1.0;
    double h = 1.0;
    String criterion,result;
    
    boolean checkName = false;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_account);
        
        ImageButton btnSave = (ImageButton)findViewById(R.id.btnSave);
          
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);
        editText3 = (EditText)findViewById(R.id.editText3);
        
        male = (RadioButton)findViewById(R.id.radioButton1);
        female = (RadioButton)findViewById(R.id.radioButton2);
        
        this.dh = new DataHelper(this);
        allData = this.dh.selectAll();
        
        
        
        btnSave.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
			
			if( (editText1.getText().length() == 0) ||
					(editText2.getText().length() == 0 ) ||
					(editText3.getText().length() == 0) ){
				
				alertDataBlank();
				
			}
			else{
				
				for (String name : allData) {
			           if( name.equals(editText1.getText().toString()))
			           {  
			           	  	checkName = true;
			           	  	
			           }  
			           break;
				}
				if(checkName) {
					
					alertNameValid();
					checkName = false;
				}
				else{
	        	   // TODO Auto-generated method stub
					//EditText value = (EditText) findViewById(R.id.editText1);
					float valuew = Float.valueOf(editText2.getText().toString()).floatValue();
					w = (double) valuew;
					
					
					//EditText value2 = (EditText) findViewById(R.id.editText2);
					float valueh = Float.valueOf(editText3.getText().toString()).floatValue();
					h = (double) valueh;
					
					result = calbmi(w,h);

					chooseChoice();
					
					Locale lc = new Locale("th","TH");
					//System.out.println(new SimpleDateFormat("dd/MM/yyyy",lc).format(new Timestamp(System.currentTimeMillis())));
					
			        final Calendar c = Calendar.getInstance();
			        
			        int mYear = c.get(Calendar.YEAR);
			        int mMonth = c.get(Calendar.MONTH);
			        int mDay = c.get(Calendar.DAY_OF_MONTH);
			        String date = Integer.toString(mDay);
			        date = date+"-"+Integer.toString(mMonth);
			        strInput7 = date+"-"+Integer.toString(mYear);
			        
			         long time_current = System.currentTimeMillis();
			        
					//Date date_now = new Date(mDay, mMonth, mYear);
			        
			        //DATE FUALT!!!!????
					
					launch_confirmdata = new Intent(getApplicationContext(),  ConfirmData.class); 
					strInput1 = editText1.getText().toString();
					strInput2 = editText2.getText().toString();
					strInput3 = editText3.getText().toString();
					strInput4 = chooseChoice();
					strInput5 = result;
					strInput6 = "0";
					//strInput7 = date_now;

					launch_confirmdata.putExtra("strInput1", strInput1);
					launch_confirmdata.putExtra("strInput2", strInput2);
					launch_confirmdata.putExtra("strInput3", strInput3);
					launch_confirmdata.putExtra("strInput4", strInput4);
					launch_confirmdata.putExtra("strInput5", strInput5);
					launch_confirmdata.putExtra("strInput6", strInput6);
					launch_confirmdata.putExtra("strInput7", strInput7);// Sent Data in strInput to ShowData
							 
					
					startActivity(launch_confirmdata);
	        	   
	           }
				
			}
							
		}});
				
    }
    
    public String chooseChoice()
    {  String sex = new String();
    	if(male.isChecked()){
    		sex = "male";
    	}
    	else if(female.isChecked()){
    		sex = "female";
    	}
    	return sex;

    }
    
    
    
    public String calbmi (double weight,double height){
    	
    	bmi = (weight/(height*height))*10000; 

    	if(bmi<18.5)
    	 { 
    		criterion = "Underweight";
    	 }
    	
    	else if (bmi>=18.5 && bmi<=24.9)
    	{
    		criterion = "Normalweight ";
    	}
    	
    	else if (bmi>=25 && bmi<=29.9)
    	{
    		criterion = "Overweight";
    	}
    	
    	
    	else //(bmi>=30)
    	{
    		criterion = "Obesity";
    	}
    	

    	return(criterion);
    }
    
    
    
    public void alertNameValid(){
    
              alert = new AlertDialog.Builder(this);
              alert.setMessage("This name is already used. Please,Try again.");
              alert.setCancelable(false);
              alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					dialog.dismiss();
				}
			});
              
              alert.show();
     }
    
    
    public void alertDataBlank(){
    
              alert2 = new AlertDialog.Builder(this);
              alert2.setMessage("Please,Enter your data");
              alert2.setCancelable(false);
              alert2.setPositiveButton("OK",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					dialog.dismiss();
				}
			});
              
              alert2.show();
     }

    
}
